import {
  Entity,
  Column,
  PrimaryGeneratedColumn,
  CreateDateColumn,
  UpdateDateColumn,
  ManyToOne,
  AfterLoad
} from "typeorm"
import { User } from "./User";
import { Article } from "./Article";

import moment from "moment/moment"
import { Post } from "./Post";

moment.locale('zh-cn')

@Entity()
export class Comment {
  @PrimaryGeneratedColumn()
  id: number // 评论id

  @Column({ type: "text", comment: "评论内容" })
  content: string // 评论内容

  @Column({ default: 0, comment: "点赞数" })
  likeCount: number // 点赞数

  @ManyToOne(() => Article, { createForeignKeyConstraints: false, nullable: true })
  article: Article // 所属文章

  @ManyToOne(() => Post, { createForeignKeyConstraints: false, nullable: true })
  post: Post // 所属帖子

  @ManyToOne(() => User, { createForeignKeyConstraints: false })
  commenter: User // 评论者

  @CreateDateColumn()
  createdAt: Date | string; // 评论发布时间

  @UpdateDateColumn()
  updatedAt: Date | string; // 评论更新时间

  @AfterLoad()
  formatDate() {
    this.createdAt = moment(this.createdAt).format('LL')
    this.updatedAt = moment(this.updatedAt).format('LL')
  }
}